package de.velvettear.squirrel.util;

import java.io.File;
import java.io.RandomAccessFile;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class BitmapHelper {

	public static Bitmap createBitmap(Context context, String path) {
		Bitmap bitmap = null;
		try {
			File file = new File(path);
			RandomAccessFile raf = new RandomAccessFile(file, "r");
			
			long longLength = raf.length();
			int length = (int) longLength;
				
			byte[] data = new byte[length];
			raf.readFully(data);
				
			int iconSize = getLauncherIconSize(context);
				
			bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
			bitmap = Bitmap.createScaledBitmap(bitmap, iconSize, iconSize, true);	
		} catch (Exception e) {
			if (e.getMessage() != null) {
				Log.e("getCurrentFragment()", e.getMessage());				
			}
		}
		return bitmap;
	}
					
	public static int getLauncherIconSize(Context context) {
		ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
	    return activityManager.getLauncherLargeIconSize();
	}
	
}
